// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#ifndef OpenMeca_Item_Body_hpp
#define OpenMeca_Item_Body_hpp

#include "OpenMeca/Core/UserRootItem.hpp"
#include "OpenMeca/Core/AutoRegister.hpp"
#include "OpenMeca/Gui/UserItemDialogBody.hpp"
#include "OpenMeca/Util/Color.hpp"

#include "OpenMeca/Geom/Point.hpp"
#include "OpenMeca/Geom/Quaternion.hpp"
#include "OpenMeca/Geom/Frame.hpp"
#include "OpenMeca/Geom/Matrix.hpp"

namespace OpenMeca
{  
  namespace Core
  {
    class Action;
  }
}



namespace OpenMeca
{  
  namespace Item
  {

    class Part;
    class PartUser;

    class Body: public Core::UserRootItem, public Core::AutoRegister<Body>
    {
    
    public:
      static const std::string GetStrType(); 
      static void Init();

      friend class  Gui::UserItemDialogBody;
      typedef Gui::UserItemDialogBody Dialog;

      static void DrawIcon(QIcon&, QColor, bool fixed);
      
    public:
      Body();
      ~Body();
    
      const double &GetMass() const;
      double &GetMass();


      const Geom::Point<_3D>& GetMassCenter() const;
      Geom::Point<_3D>& GetMassCenter();

      const Geom::Quaternion<_3D>& GetQuaternion() const;
      Geom::Quaternion<_3D>& GetQuaternion();

      const Geom::Frame<_3D>& GetFrame() const;
      Geom::Frame<_3D>& GetFrame();

      const Geom::Matrix<_3D>& GetInertia() const;
      Geom::Matrix<_3D>& GetInertia();


      const Util::Color& GetColor() const;
      Util::Color& GetColor();

      const OpenMeca::Item::Body& GetBody() const;
      OpenMeca::Item::Body& GetBody();

      const bool& IsFixed() const;
      bool& IsFixed();

      void ToggleFix();

      Core::SetOfBase<Part> GetParts();

    private:
      Body(const Body&);             //Not Allowed
      Body& operator=(const Body&);  //Not Allowed

      void UpdateIcon();
      Core::SetOfBase<Core::UserItem> GetAssociatedSelectedItem();
      
      friend class boost::serialization::access;
      template<class Archive>
      void serialize(Archive & ar, const unsigned int);

    private:
      Util::Color color_;
      double mass_;
      Geom::Point<_3D> massCenter_;
      Geom::Quaternion<_3D> quaternion_;
      Geom::Frame<_3D> frame_;
      Geom::Matrix<_3D> inertia_;
      bool fixed_;
    };


    template<class Archive>
    inline void
    Body::serialize(Archive & ar, const unsigned int)
      {
	ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Core::UserRootItem);
	ar & BOOST_SERIALIZATION_NVP(color_);
	ar & BOOST_SERIALIZATION_NVP(mass_);
	ar & BOOST_SERIALIZATION_NVP(massCenter_);
	ar & BOOST_SERIALIZATION_NVP(quaternion_);
	ar & BOOST_SERIALIZATION_NVP(inertia_);
	ar & BOOST_SERIALIZATION_NVP(fixed_);
      }



  }

}


namespace OpenMeca
{  
  namespace Core
  {

    class Action_FixBody
    {
    public:
      static QString Text();
      static QIcon Icon();
      static void DoAction(OpenMeca::Item::Body&, Action& action);
    };

  }
}



#endif
